<?php

namespace catchAdmin\apipatient\controller;

use app\common\business\Contract;
use catchAdmin\apipatient\service\MyService;
use catcher\exceptions\FailedException;
use think\Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use app\common\model\PatientInfo as PatientInfoModel;
use app\common\model\Relation as RelationModel;
use app\common\model\Source as SourceModel;
use catchAdmin\apipatient\validate\MyValidate;
use think\exception\ValidateException;

class My extends CatchController
{
    /**
     * 获取我的信息
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_info(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(MyValidate::class)->scene('get_my_info')->check($req);
            return CatchResponse::success((new PatientInfoModel)->getPatientInfo(request()->userInfo['patientId'], $req['type']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取家庭成员
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_family(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            return CatchResponse::success((new PatientInfoModel)->getPatientIdRelation(request()->userInfo['patientId']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 添加家庭成员
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function my_family_add(Request $request): \think\response\Json
    {
        return CatchResponse::success((new PatientInfoModel())->addNumber(request()->userInfo['id'], $request->param()));
    }

    /**
     * 更新家庭成员
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function my_family_up(Request $request): \think\response\Json
    {
        return CatchResponse::success((new PatientInfoModel)->upNumber(request()->userInfo['id'], $request->param()));
    }

    /**
     * 删除家庭成员
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function my_family_del(Request $request): \think\response\Json
    {
        return CatchResponse::success((new PatientInfoModel)->delNumber(request()->userInfo['id'], $request->param()));
    }

    /**
     * 获取家庭成员详情
     *
     * @time 2020年01月09日
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_family_detailed(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(MyValidate::class)->scene('get_my_family_detailed')->check($req);
            return CatchResponse::success((new PatientInfoModel)->getRelationDetailed(request()->userInfo['patientId'], $req['sid']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 获取关系
     * @return \think\response\Json
     */
    public function get_hzd_relation(): \think\response\Json
    {
        return CatchResponse::success((new RelationModel)->getDatas());
    }

    /**
     * 获取来源
     * @return \think\response\Json
     */
    public function get_hzd_source(): \think\response\Json
    {
        return CatchResponse::success((new SourceModel)->getDatas());
    }

    public function contact_customer_service()
    {
        $data = [
            'title' => '联系客服',
            'summary' => '若您对服务有任何疑问，可联系健康管家免费咨询',
            'mobile' => '13756755835',
            'wechat' => '/files/20221107/abcd.png',
            'working_time' => '9:00-17:00'
        ];
        return CatchResponse::success($data);
    }

    public function getMyContractList(Request $request): \think\response\Json
    {

        $patientId = $request->param("patient_id", $request->userInfo["patientId"], "string");
        if (empty($patientId)) {
            throw new FailedException("患者不存在");
        }
        //根据患者ID获取数据列表
        $result = (new Contract())->getListByPatientId($patientId);

        return CatchResponse::paginate($result);
    }


    /**
     * 获取签约医生列表
     * @param Request $request
     */
    public function accessToContractedDoctors(Request $request): \think\response\Json
    {
        $doctorIds = $request->param("doctor_ids", [], "string");
        $doctorList = (new MyService())->getDoctorsByIds($doctorIds);
        return CatchResponse::success($doctorList);
    }


}